﻿using CodeBuilderForVS2022.Entity;
using EnvDTE;
using EnvDTE80;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;

namespace CodeBuilderForVS2022
{
    public partial class Main : Form
    {
        /// <summary>
        /// 选择的数据表
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 数据表的注释
        /// </summary>
        public string TableDesc { get; set; }

        /// <summary>
        /// DTE
        /// </summary>
        public DTE2 Dte { get; set; }

        public Main(string tableName, string tableDesc, DTE2 dte)
        {
            InitializeComponent();
            TableName = tableName;
            TableDesc = tableDesc;
            Dte = dte;
            if (string.IsNullOrEmpty(TableName))
            {
                labelDesc.Text = "";
            }
            else
            {
                labelDesc.Text = TableDesc;
            }
        }

        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuild_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                return;
            }
            MessageBox.Show("开始啦");
        }

        /// <summary>
        /// 选择model
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 新增方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                return;
            }
        }

        /// <summary>
        /// 删除代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                return;
            }
        }

        /// <summary>
        /// 改变加载状态
        /// </summary>
        /// <param name="log">日志</param>
        /// <param name="num">第几部</param>
        public void ShowLoading(string log)
        {
            lblState.Text = log;
        }

        /// <summary>
        /// 生成异常
        /// </summary>
        /// <param name="log">日志</param>
        public void ShowError(string log)
        {
            lblState.Text = log;
        }

        private bool Check()
        {
            if (string.IsNullOrEmpty(TableName))
            {
                MessageBox.Show("请先选择一个Model");
                return true;
            }
            return false;
        }

        /// <summary>
        /// 跳转首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://flyadmin.top");
        }


        /// <summary>
        /// 获取Api所在的目录
        /// </summary>
        /// <param name="topProject"></param>
        /// <returns></returns>
        public string GetApiDir(Project topProject)
        {
            var arr = topProject.FileName.Split('\\');
            string projFullName = arr[arr.Length - 2];
            string projName = projFullName.Split('.')[1];
            projFullName = projFullName.Replace(projName, "WebApi");
            var newList = arr.ToList();
            newList.RemoveAt(arr.Length - 1);
            newList.RemoveAt(arr.Length - 2);
            newList.Add(projFullName);
            return string.Join("\\", newList);
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }
    }
}
